package com.inhg.inhg_android.net;

import com.inhg.inhg_android.net.error.ApiException;
import com.inhg.inhg_android.net.error.NetWorkException;
import com.inhg.inhg_android.net.interfaces.ErrorIF;
import com.orhanobut.logger.Logger;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by ZengYan on 2017/7/25.
 * Email : 810989881@qq.com
 *
 * @decs : 做错误处理
 */

public abstract class BaseObserver<T> implements Observer<T>,ErrorIF {


    @Override
    public void onSubscribe(@NonNull Disposable disposable) {
    }



    @Override
    public void onError(@NonNull Throwable e) {
        Logger.e(e,e.getMessage());
        if (e instanceof SocketTimeoutException) {
            showError("连接超时");
        } else if (e instanceof ConnectException) {
            showError("连接失败");
        } else if (e instanceof UnknownHostException) {
            showError("找不到服务器");
        } else if (e instanceof NetWorkException){
            showError("当前网络不可用");
        } else if (e instanceof ApiException) {
            showError("错误码："+((ApiException) e).getCode()+"\n"+"message:"+e.getMessage());
        }else {
            showError("其他错误");
        }

    }

    @Override
    public void onComplete() {

    }
}
